package club.hicode.daydayup.monitor;

import cn.hutool.core.util.StrUtil;
import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.text.DecimalFormat;


public class ProcessResourceCollector {

	public static DecimalFormat FORMAT = new DecimalFormat("0.00");
	private static final OsInfo osInfo = new OsInfo();

	public static ProcessInfo getAppProcessInfo(String appName, String id, MonitorInfoBean nodeinfo) {
		ProcessInfo pidInfo = null;
		Process proc = null;
		//判定是否是linux

		if (osInfo.isLinux()) {
			try {
				String pid = "";
				if (StrUtil.isNotBlank(id)) {
					String procCmd = "/bin/sh lsof -i:" + id;
					proc = Runtime.getRuntime().exec(procCmd);
					proc.getOutputStream().close();
					BufferedReader ir = new BufferedReader(new InputStreamReader(proc.getInputStream()));
					String line = null;
					while ((line = ir.readLine()) != null) {
						if (StrUtil.isBlank(line)) {
							continue;
						}
						line = line.trim();
						pid = line.substring(line.indexOf(" "));
						pid = pid.substring(pid.lastIndexOf(" "));
						if (StrUtil.isNotBlank(pid)) {
							break;
						}
					}
				}
				pidInfo = LinuxProcessCollector.readProcessNamePidsInfo(appName, pid.trim());
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				//				proc.destroy();
			}
		} else {
			WinProcessCollector winp = new WinProcessCollector();
			try {
				String pid = "";
				if (StrUtil.isNotBlank(id)) {
					String procCmd = "cmd /c netstat -aon | findstr \"" + id + "\"";
					proc = Runtime.getRuntime().exec(procCmd);
					proc.getOutputStream().close();
					BufferedReader ir = new BufferedReader(new InputStreamReader(proc.getInputStream()));
					String line = null;
					while ((line = ir.readLine()) != null) {
						if (StrUtil.isBlank(line)) {
							continue;
						}
						line = line.trim();
						pid = line.substring(line.lastIndexOf(" "));
						if (StrUtil.isNotBlank(pid)) {
							break;
						}
					}
				}
				pidInfo = WinProcessCollector.readProcessNamePidsInfo(appName, pid.trim());
				if (null != pidInfo) {

					if (null == nodeinfo)
						nodeinfo = new MonitorInfoBean();
					try {
						nodeinfo = winp.getMonitorInfoBean(nodeinfo);
					} catch (Exception e1) {
						e1.printStackTrace();
					}

					if (null != nodeinfo) {
						pidInfo.memoryUsage = Double
								.valueOf(100 * pidInfo.memoryInuse / nodeinfo.SizeStoredInPagingFiles);
					}
					pidInfo.cpuUsage = winp.getCpuRatio(appName);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				proc.destroy();
			}
		}
		return pidInfo;
	}

	public static void main(String[] args) throws Exception {
		Process proc = null;
		String procCmd = "cmd /c netstat -aon | findstr \"" + 10001 + "\"";
		proc = Runtime.getRuntime().exec(procCmd);
		proc.getOutputStream().close();
		BufferedReader ir = new BufferedReader(new InputStreamReader(proc.getInputStream()));
		String line = null;
		String pid = "";
		while ((line = ir.readLine()) != null) {
			if (StrUtil.isBlank(line)) {
				continue;
			}
			System.out.println(line);
			line = line.trim();
			pid = line.substring(line.lastIndexOf(" "));
			if (StrUtil.isNotBlank(pid)) {
				break;
			}
		}
		System.out.println(pid.trim());
	}

	public static MonitorInfoBean getNodeSysInfo(MonitorInfoBean nodeSysInfo) {
		try {
			return SystemTypeFactory.getSystemCollector().getMonitorInfoBean(nodeSysInfo);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return nodeSysInfo;
	}

}
